package com.webserver.http;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

import static java.sql.DriverManager.println;

/**
 * 响应对象
 * 该类的每一个实例用于表示HTTP协议要求的响应
 * 每个响应由三部分组成:
 * 状态行，响应头，响应正文
 */
public class HttpServlerResponse {
    private Socket socket;
    //状态行相关信息
    private int statusCode = 200;
    private String statusReason = "OK";

    public HttpServlerResponse(Socket socket) {
        this.socket = socket;
    }

    //响应头相关信息

    //响应正文相关信息
    private File contentFile;//正文的文件


    /**
     * 将当前响应对象内容按照标准的HTTP响应格式发送给客户端
     */
    public void response() throws IOException {
        /**
         *
         */

        sendStatusLine();
        sendStatusLine();
        sebdContent();


    }

    /**
     * 发送状态行
     */
    private void sendStatusLine() throws IOException {
        //3.1发送状态行
        println("HTTP/1.1" + " " + statusCode + " " + statusReason);

    }

    /**
     * 发送响应头
     */
    private void sendHeaders() throws IOException {
        //3.2发送响应头
        println("Content-Type: text/html");
        println("Content-Length: " + contentFile.length());
        println("");
    }

    /**
     * 发送响应正文
     */
    private void sebdContent() throws IOException {
        //3.3发送响应正文(index.html页面的数据)
        OutputStream out = socket.getOutputStream();
        FileInputStream fis = new FileInputStream(contentFile);
        int len;
        byte[] data = new byte[1024 * 10];
        while ((len = fis.read(data)) != -1) {
            out.write(data, 0, len);
        }
        }

        /**
         * 将一行字符串按照ISO8859-1编码发送给客户端,最后会自动添加回车和换行符
         * @param line
         */
        private void println (String line) throws IOException {
            OutputStream out = socket.getOutputStream();
            byte[] data = line.getBytes(StandardCharsets.ISO_8859_1);
            out.write(data);
            out.write(13);//单独发送了回车符
            out.write(10);//单独发送换行符
        }

        public void setStatusCode ( int statusCode){
            this.statusCode = statusCode;
        }

        public void setStatusReason (String statusReason){
            this.statusReason = statusReason;
        }

        public void setContentFile (File contentFile){
            this.contentFile = contentFile;
        }
    }
